﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXShop.IDAL.SystemInfo;
using System.Data;
using System.Data.SqlClient;

namespace YXShop.SQLServerDAL.SystemInfo
{
    public class SMS:ISMS
    {
        public SMS()
        {
        }


        public int Add(Model.SystemInfo.SMS model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [yxs_sms](");
            strSql.Append("[phone],[message],[sendtime],[sendtype],[sendstatus],[sendid])");
            strSql.Append("values(");
            strSql.Append("@phone,@message,@sendtime,@sendtype,@sendstatus,@sendid) Select scope_IDENTITY()");
            SqlParameter[] parameters = (SqlParameter[])this.ValueParams(model);
            object obj = ChangeHope.DataBase.SQLServerHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public void Update(Model.SystemInfo.SMS model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [yxs_sms] set ");
            strSql.Append("[phone]=@phone,[message]=@message,[sendtime]=@sendtime,[sendtype]=@sendtype,[sendstatus]=@sendstatus,[sendid]=@sendid  ");
            strSql.Append("where [id]=@id ");
            SqlParameter[] parameters = (SqlParameter[])this.ValueParams(model);
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        public void Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [yxs_sms] ");
            strSql.Append("where [id]=@id");
            SqlParameter[] parameters ={
                                          new SqlParameter("@id",SqlDbType.Int,4)
                                      };
            parameters[0].Value = id;
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        public void DeleteAll(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [yxs_sms] where [id] in(");
            strSql.Append(ids + " )");
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString());
        }

        public Model.SystemInfo.SMS GetModelById(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 [phone],[message],[sendtime],[sendtype],[sendstatus],[sendid]  from [yxs_sms] ");
            strSql.Append("where [id]=@id");
            SqlParameter[] parameters ={
                                          new SqlParameter("@id",SqlDbType.Int,4)
                                      };
            parameters[0].Value = id;
            YXShop.Model.SystemInfo.SMS model = null;
            using (SqlDataReader reader = ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString(), parameters))
            {
                while (reader.Read())
                {
                    model = new YXShop.Model.SystemInfo.SMS();
                    model.ID = (int)reader["id"];
                    model.Phone = (string)reader["phone"];
                    model.Message = (string)reader["message"];
                    model.Sendtime = (DateTime)reader["sendtime"];
                    model.Sendtype = (int)reader["sendtype"];
                    model.Sendstatus = (int)reader["sendstatus"];
                    model.Sendid = (int)reader["sendid"];
                }
            }
            return model;
        }

        public List<Model.SystemInfo.SMS> GetByWhere(string where)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [phone],[message],[sendtime],[sendtype],[sendstatus],[sendid]  from [yxs_sms] ");
            strSql.Append("where 1=1 "+ where);
            List<YXShop.Model.SystemInfo.SMS> models = new List<Model.SystemInfo.SMS>();
            using (SqlDataReader reader = ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString()))
            {
                while (reader.Read())
                {
                    YXShop.Model.SystemInfo.SMS model = new YXShop.Model.SystemInfo.SMS();
                    model.ID = (int)reader["id"];
                    model.Phone = (string)reader["phone"];
                    model.Message = (string)reader["message"];
                    model.Sendtime = (DateTime)reader["sendtime"];
                    model.Sendtype = (int)reader["sendtype"];
                    model.Sendstatus = (int)reader["sendstatus"];
                    model.Sendid = (int)reader["sendid"];
                    models.Add(model);
                }
            }
            return models;
        }

        public ChangeHope.DataBase.DataByPage GetListByWhere(string where)
        {
            string sql = "[select] * [from] yxs_sms [where] 1=1 "+where +" [order by] id DESC ";
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = sql;
            dataPage.GetRecordSetByPage();
            return dataPage;
        }

        public ChangeHope.DataBase.DataByPage GetList(string orderfield, int pagesize, string Conditions)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] * [from] yxs_sms [where] 1=1 " + Conditions + " " + orderfield;
            dataPage.PageSize = pagesize;
            dataPage.GetRecordSetByPage();
            return dataPage;
        }

        public IDataParameter[] ValueParams(YXShop.Model.SystemInfo.SMS model)
        {
            SqlParameter[] parameters = new SqlParameter[7];
            parameters[0] = new SqlParameter("@id", SqlDbType.Int, 4);
            parameters[1] = new SqlParameter("@phone", SqlDbType.Char, 20);
            parameters[2] = new SqlParameter("@message", SqlDbType.NVarChar, 300);
            parameters[3] = new SqlParameter("@sendtime", SqlDbType.DateTime);
            parameters[4] = new SqlParameter("@sendtype", SqlDbType.Int, 4);
            parameters[5] = new SqlParameter("@sendstatus", SqlDbType.Int, 4);
            parameters[6] = new SqlParameter("@sendid", SqlDbType.Int, 4);
            parameters[0].Value = model.ID;
            parameters[1].Value = model.Phone;
            parameters[2].Value = model.Message;
            parameters[3].Value = model.Sendtime;
            parameters[4].Value = model.Sendtype;
            parameters[5].Value = model.Sendstatus;
            parameters[6].Value = model.Sendid;
            return parameters;

        }
    }
}
